<template>
  <div class="roleManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="aler('')">
          导出
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.permission"
              placeholder="请输入查询条件"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      @selection-change="setSelectRows"
    >
      <el-table-column show-overflow-tooltip type="selection"></el-table-column>
      <el-table-column
        prop="code"
        label="规则编码"
        min-width="10%"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        prop="name"
        label="规则名称"
        min-width="10%"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        prop="expshowflag"
        label="规则描述"
        min-width="25%"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        prop="ctime"
        label="报警时间"
        min-width="7%"
      ></el-table-column>
      <el-table-column
        prop="alarmvalue"
        label="报警值"
        min-width="12%"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column prop="lvltext" label="报警级别" min-width="10%">
        <template #default="scope">
          <p v-if="scope.row.level == '1'">
            <el-button size="mini" type="warning" disabled>预警</el-button>
          </p>
          <p v-if="scope.row.level == '2'">
            <el-button size="mini" type="danger" disabled>报警</el-button>
          </p>
        </template>
      </el-table-column>
      <el-table-column
        prop="suremsg"
        label="确认信息"
        min-width="11%"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="200">
        <template #default="scope">
          <p v-if="scope.row.status == '0'">
            <el-button
              type="warning"
              size="mini"
              @click="waitConfirm(scope.row)"
            >
              报警待确认
            </el-button>
          </p>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <edit ref="edit" @fetch-data="fetchData"></edit>
  </div>
</template>

<script>
  import { getAlarmLogList, saveAlarmReason } from '@/api/alarm'
  import Edit from './components/RoleManagementEdit'

  export default {
    name: 'RoleManagement',
    components: { Edit },
    data() {
      return {
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          permission: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      waitConfirm(row) {
        console.log(row)
        if (row) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.own_id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ id: row.own_id })
            this.$baseMessage(msg, 'success')
            this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.own_id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ id: ids })
              this.$baseMessage(msg, 'success')
              this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getAlarmLogList(this.queryForm)
        this.list = data.list
        this.total = data.pageInfo.data.totalRows
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
